﻿using SMART_VIET_V01.Lib;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace SMART_VIET_V01
{
    public partial class frm_themtuongtac : Form
    {
        frm_main parent;
        string monhoc_id;
        DataTable list_tuongtac_hientai;
        SQL_Functions funs = new SQL_Functions();
        public frm_themtuongtac()
        {
            InitializeComponent();
        }
        public frm_themtuongtac(frm_main parent, string monhoc_id, DataTable list_tuongtac_hientai)
        {
            this.parent = parent;
            this.monhoc_id = monhoc_id;
            this.list_tuongtac_hientai = list_tuongtac_hientai;
            InitializeComponent();
            loadTuongtac(monhoc_id);
            loadTuongtachientai();
        }
        #region Loading
        Hashtable list_thuvien_tuongtac = new Hashtable();
        void loadTuongtac(string monhoc_id)
        {
            string sql = "SELECT * FROM  [tblTuongTacLib] where monhoc_id = " + monhoc_id;
            DataTable dt = funs.returnDB(sql);
            string sqlgr = "SELECT * FROM  [tblLoaiTuongTac] ";
            DataTable dtGroup = funs.returnDB(sqlgr);
            foreach (DataRow item in dtGroup.Rows)
            {
                ListViewGroup gr = new ListViewGroup();
                gr.Header = item["ten_loai"].ToString();
                gr.Tag = item["stt"].ToString();
                thuvien_tuongtac.Groups.Add(gr);
            }
            foreach (DataRow item in dt.Rows)
            {
                ListViewItem tuongtac = new ListViewItem();
                tuongtac.Text = item["ten_tuongtac"].ToString();
                ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
                subitem.Text = item["stt"].ToString();
                int loaituongtac = 0;
                Int32.TryParse(item["loaituongtac_id"].ToString(), out loaituongtac);
                switch (loaituongtac)
                {
                    case 1:
                        tuongtac.StateImageIndex = 0;
                        break;
                    case 2:
                        tuongtac.StateImageIndex = 1;
                        break;
                    default:
                        tuongtac.StateImageIndex = 2;
                        break;
                }
                foreach (ListViewGroup gr in thuvien_tuongtac.Groups)
                {
                    if (gr.Tag.ToString().Equals(item["loaituongtac_id"].ToString()))
                    {
                        gr.Items.Add(tuongtac);
                    }
                }
                thuvien_tuongtac.Items.Add(tuongtac);
            }
        }
        void loadTuongtachientai()
        {
            //string sql = "SELECT * FROM  [tblTuongTacLib] where monhoc_id = 5";
            //DataTable list_tuongtac_hientai = funs.returnDB(sql);
            if (list_tuongtac_hientai == null) list_tuongtac_hientai = new DataTable();
            string sqlgr = "SELECT * FROM  [tblLoaiTuongTac] ";
            DataTable dtGroup = funs.returnDB(sqlgr);
            foreach (DataRow item in dtGroup.Rows)
            {
                ListViewGroup gr = new ListViewGroup();
                gr.Header = item["ten_loai"].ToString();
                gr.Tag = item["stt"].ToString();
                tuongtac_hientai.Groups.Add(gr);
            }
            foreach (DataRow item in list_tuongtac_hientai.Rows)
            {
                ListViewItem tuongtac = new ListViewItem();
                tuongtac.Text = item["ten_tuongtac"].ToString();
                ListViewItem.ListViewSubItem subitem = new ListViewItem.ListViewSubItem();
                subitem.Text = item["stt"].ToString();
                int loaituongtac = 0;
                Int32.TryParse(item["loaituongtac_id"].ToString(), out loaituongtac);
                switch (loaituongtac)
                {
                    case 1:
                        tuongtac.StateImageIndex = 0;
                        break;
                    case 2:
                        tuongtac.StateImageIndex = 1;
                        break;
                    default:
                        tuongtac.StateImageIndex = 2;
                        break;
                }
                foreach (ListViewGroup gr in tuongtac_hientai.Groups)
                {
                    if (gr.Tag.ToString().Equals(item["loaituongtac_id"].ToString()))
                    {
                        gr.Items.Add(tuongtac);
                    }
                }
                tuongtac_hientai.Items.Add(tuongtac);
            }
        }
        bool kiemtradub(string id, ListView target)
        {
            bool found = false;

            foreach (ListViewItem item in target.Items)
            {
                if (item.SubItems[0].Text == id)
                { found = true; }
            }
            return found;
        }
        #endregion

        private void bt_themtuongtac_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in thuvien_tuongtac.SelectedItems)
            {
                if (!kiemtradub(item.SubItems[0].Text, tuongtac_hientai))
                {
                    foreach (ListViewGroup gr in tuongtac_hientai.Groups)
                    {
                        if (item.Group.Tag.ToString().Equals(gr.Tag.ToString()))
                        {
                            gr.Items.Add((ListViewItem)item.Clone());
                        }
                    }
                    tuongtac_hientai.Items.Add((ListViewItem)item.Clone());
                }
            }
        }

        private void bt_gobotuongtac_Click(object sender, EventArgs e)
        {
            foreach (ListViewItem item in tuongtac_hientai.SelectedItems)
            {
                item.Remove();
            }
        }

        private void bt_xoatuongtac_Click(object sender, EventArgs e)
        {
            //list_tuongtac_hientai.Clear();
            tuongtac_hientai.Clear();
        }

        private void bt_chapnhan_Click(object sender, EventArgs e)
        {
            list_tuongtac_hientai.Clear();
            if (!list_tuongtac_hientai.Columns.Contains("stt"))
            {
                list_tuongtac_hientai.Columns.Add(new DataColumn("stt", typeof(string)));
                list_tuongtac_hientai.Columns.Add(new DataColumn("ten_tuongtac", typeof(string)));
                list_tuongtac_hientai.Columns.Add(new DataColumn("loaituongtac_id", typeof(string)));
            }
            foreach (ListViewItem item in tuongtac_hientai.Items)
            {
                DataRow dr = list_tuongtac_hientai.NewRow();
                dr["stt"] = item.SubItems[0].Text;
                dr["ten_tuongtac"] = item.Text;
                dr["loaituongtac_id"] = item.Group.Tag.ToString();
                list_tuongtac_hientai.Rows.Add(dr);
            }
            parent.loadTuongtac(list_tuongtac_hientai);
            this.Close();
        }

        private void bt_huy_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}
